<?php

// namespace Filter;

//引入参数文件进行测试
// include_once('./Params.php');

/*
|Filter 过滤类
|描述：用于请求的变量检测和处理引导
|接口规范：url?role=Customer&action=checkRegiste&openid=111&[更多的三级参数]
|补充：role和action参数是必要的，三级参数会根据查询的数据所需而变化
|     这是一个过滤用的类，只是响应一二级的缺失报错
*/
class Filter{

	//一级参数数组：角色
	var $ParamsRole = array('Manager','Business','Customer');

	//二级参数数组：行动列表
	var $ParamsActionM = array(
		'login',
		'registe'
	);//ManagerActionWords
	var $ParamsActionB = array(
		'login',
		'registe',
		'checkGoods',
		'addGood',
		'deleteGood',
		'checkOrders',
		'dealOrder'
	);//BusinessActionWords
	var $ParamsActionC = array(
		'login',
		'autoLogin',
		'addShopCart',
		'addComment',
		'alterClientInfo',
		'checkCode',
		'checkAdd',
		'checkShopCart',
		'checkBasicInfo',
		'checkGoods',
		'checkComment',
		'getGoodDetail',
		'getPageConfigData',
		'insertHimage',
		'deleteShopCart',
		'addOrder',
		'checkOrder',
		'comfirmOrder',
		'searchGoods'
	);//CustomerActionWords

	public function __construct(){
		if($this->requestDetecte()){
			//continue...
			include_once('./Controller.php');
			$role = $_POST['role'];
			$action = $_POST['action'];
			$con = new Controller($role,$action);
		}
	}

	private function requestDetecte(){
		//参数检查
		//两个参数是否存在？并获取
		if(isset($_POST['role'])&&isset($_POST['action'])){
			$role = $_POST['role'];
			$action = $_POST['action'];
		}else{
			echo '{
				"re":"Miss params:role or action"
			}';
			return false;
		}
		//role正确吗?
		if( is_int(array_search($role,$this->ParamsRole))!=true ){
			echo '{
				"re":"Unknow role"
			}';
			return false;
		}
		//action正确吗？
		switch($role){
			case'Customer':
				$acFlag=is_int(array_search($action,$this->ParamsActionC));
				break;
			case'Business':
				$acFlag=is_int(array_search($action,$this->ParamsActionB));
				break;
			case'Manager':
				$acFlag=is_int(array_search($action,$this->ParamsActionM));
				break;
			default:
				break;
		}
		if($acFlag==false){
			echo '{
				"re":"Unknow action"
			}';
			return false;
		}
		return true;
	}
}

$filt = new Filter;
?>